'use strict';

angular.module('BC')
    .factory('DataPushService', ['$q', 'Authentication', 'BCService', 'DataPushConst',
        function ($q, Authentication, BCService, DataPushConst) {

            var o = {};

            //数据同步
            o.dataSync = function dataSync(entityFullName) {
                var defer = $q.defer();

                //获取调用U9服务的上下文信息
                BCService.getERPContext(function (err, context) {
                    if (err != null) {
                        defer.reject(err);
                    }
                    else {
                        var params = {
                            context: context,
                            entityFullName: entityFullName
                        };
                        //调用U9服务 获取需要同步的数据    
                        BCService.httpPOSTErpSV(DataPushConst.dataSyncURL, params, function (err, result) {
                            if (err != null) {
                                defer.reject(err);
                            }
                            else {
                                //保存需要同步的数据
                                BCService.saveSyncData(result, entityFullName, function (err, result) {
                                    if (err != null) {
                                        defer.reject(err);
                                    }
                                    else {
                                        defer.resolve(result);
                                    }
                                });
                            }

                        });

                    }
                });

                return defer.promise;
            }

            return o;
        }
    ]);